package com.jd.mysql.mgr.controller.account;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jd.mysql.mgr.dto.MgrAccountDTO;
import com.jd.mysql.mgr.pojo.MgrAccount;
import com.jd.mysql.mgr.pojo.MgrAccountGroup;
import com.jd.mysql.mgr.service.IMgrAccountService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.MD5Util;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.StringUtil;

public class AccountController extends MultiActionController {
	
	private static Integer start=0;
	private static Integer limit=GlobalStaticVariables.limit;
	
	public IMgrAccountService mgrAccountService;
	
	public void setMgrAccountService(IMgrAccountService mgrAccountService) {
		this.mgrAccountService = mgrAccountService;
	}
	

	/**
	 * Add dba.
	 * @deprecated
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView addAccount(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String passwordAgain = request.getParameter("passwordAgain");
		String email = request.getParameter("email");
		if(!password.equals(passwordAgain)){
			request.setAttribute("msg", "Different passwords!");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(username)){
			request.setAttribute("msg", "Username is null,please input legal username");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(password)){
			request.setAttribute("msg", "Password is null,please input legal password");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(passwordAgain)){
			request.setAttribute("msg", "The second password is null,please input legal password again");
	        return new ModelAndView("error");
		}
		if(StringUtil.isNull(email)){
			request.setAttribute("msg", "The Email is null,please input legal Email");
	        return new ModelAndView("error");
		}
		MgrAccount mgraccountOld = mgrAccountService.findMgrAccountByUserName(username);
		if(mgraccountOld!=null){
			request.setAttribute("msg", "Replicate username,change a special one just for you!");
	        return new ModelAndView("error");
		}
		MgrAccount mgraccount = new MgrAccount();
		String passwordMd5 = MD5Util.getMD5(password);
		mgraccount.setPassword(passwordMd5);
		mgraccount.setUsername(username);
		mgraccount.setEmail(email);
		mgraccount.setLastLoginIp(request.getRemoteAddr());
		mgraccount.setCreateDate(new Date());
		mgraccount.setLastLoginDate(new Date());
		mgrAccountService.addAccount(mgraccount);
		Integer groupId = (Integer)request.getSession().getAttribute("group");
		List<MgrAccount> list = mgrAccountService.getAccountList(start, limit,groupId);
		List<MgrAccountDTO> dtoList = new ArrayList<MgrAccountDTO>();
		for(MgrAccount mgra : list){
			dtoList.add(mgra.toDTO());
		}
		return null;
	//	return new ModelAndView("account/accountList","dtoList",dtoList);
	}
	
	/**
	 * delete dba
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView deleteAccount(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String accountId = request.getParameter("accountId");
		Integer id = Integer.parseInt(accountId);
		MgrAccount ma = mgrAccountService.findMgrAccountById(id);
		if(ma!=null){
			mgrAccountService.deleteAccount(ma);
		}
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, limit);
		Integer groupId = (Integer)request.getSession().getAttribute("group");
		Long totalcount = mgrAccountService.getAccountTotalCount(groupId,true);
		List<MgrAccount> list = mgrAccountService.getAccountList(start, limit,groupId);
		List<MgrAccountDTO> dtoList = new ArrayList<MgrAccountDTO>();
		for(MgrAccount mgra : list){
			dtoList.add(mgra.toDTO());
		}
		map.put("totalCount", totalcount);
		map.put("dtoList", dtoList);
		return new ModelAndView("account/accountList","map",map);
	}
	
	/**
	 * disable dba
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView disableAccount(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String accountId = request.getParameter("accountId");
		Integer id = Integer.parseInt(accountId);
		MgrAccount ma = mgrAccountService.findMgrAccountById(id);
		if(ma!=null){
			ma.setYn(false);
			mgrAccountService.updateMgrAccount(ma);
		}
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, limit);
		Integer groupId = (Integer)request.getSession().getAttribute("group");
		Long totalcount = mgrAccountService.getAccountTotalCount(groupId,true);
		List<MgrAccount> list = mgrAccountService.getAccountList(start, limit,groupId);
		List<MgrAccountDTO> dtoList = new ArrayList<MgrAccountDTO>();
		for(MgrAccount mgra : list){
			dtoList.add(mgra.toDTO());
		}
		map.put("totalCount", totalcount);
		map.put("dtoList", dtoList);
		return new ModelAndView("account/accountList","map",map);
	}
	
	/**
	 * dba list
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView accountList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Integer groupId  = (Integer)request.getSession().getAttribute("group");
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, limit);
		
		Long totalCount = mgrAccountService.getAccountTotalCount(groupId,true);
		List<MgrAccount> list = mgrAccountService.getAccountList(start, limit,groupId);
		List<MgrAccountDTO> dtoList = new ArrayList<MgrAccountDTO>();
		
		for(MgrAccount mgraccount : list){
			dtoList.add(mgraccount.toDTO());
		}
		map.put("totalCount", totalCount);
		map.put("dtoList",dtoList);
		return new ModelAndView("account/accountList","map",map);
	}

	public ModelAndView toAddAccount(HttpServletRequest request,HttpServletResponse response) throws Exception {
		List<MgrAccountGroup> list = mgrAccountService.getMgrAccountGroupList();
		String groupName = (String)request.getSession().getAttribute("groupname");
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("dtoList", list);
		map.put("groupName", groupName);
		return new ModelAndView("account/addMgrAccount","map",map);
	}
}
